package com.maint.m

import android.annotation.SuppressLint
import com.maint.m.data.model.OperationItem
import com.maint.m.TicketItem
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class TicketListAdapter(
    private val onItemClick: (TicketItem, Int) -> Unit
) : RecyclerView.Adapter<TicketListAdapter.ViewHolder>() {

    private val items = mutableListOf<TicketItem>()

    fun setItems(newItems: List<TicketItem>) {
        items.clear()
        items.addAll(newItems)
        notifyDataSetChanged()
    }

    fun addItems(newItems: List<TicketItem>) {
        val start = items.size
        items.addAll(newItems)
        notifyItemRangeInserted(start, newItems.size)
    }

    fun clear() {
        items.clear()
        notifyDataSetChanged()
    }

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val stationName: TextView = itemView.findViewById(R.id.tvStationName)
        val operationTask: TextView = itemView.findViewById(R.id.tvOperationTask)
        val status: TextView = itemView.findViewById(R.id.tvStatus)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context).inflate(R.layout.item_ticket, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = items[position]
        holder.stationName.text = item.stationName
        holder.operationTask.text = "操作任务：${item.operationTask}"
        
        // 控制"已操作"标识的显示/隐藏
        holder.status.visibility = if (item.status == "1") View.VISIBLE else View.GONE

        holder.itemView.setOnClickListener {
            onItemClick(item, position)
        }
    }

    override fun getItemCount(): Int = items.size
}

class TicketDetailAdapter(private val operationItems: List<OperationItem> = emptyList()) : RecyclerView.Adapter<TicketDetailAdapter.ViewHolder>() {

    class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val tvIndex: TextView = itemView.findViewById(R.id.tvIndex)
        val tvOperationItem: TextView = itemView.findViewById(R.id.tvOperationItem)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_operation_detail, parent, false)
        return ViewHolder(view)
    }

    @SuppressLint("SetTextI18n")
    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = operationItems[position]
        holder.tvIndex.text = "${item.number}."
        holder.tvOperationItem.text = item.content
    }

    override fun getItemCount(): Int = operationItems.size
}

